const ADSdk = require("ADSdk");
const UIManage = require("UIManage");
cc.Class({
    extends: cc.Component,

    properties: {
        desLabel_1: cc.Label,
        desLabel_2: cc.Label,
        layoutNode: cc.Node,
        playBtn: cc.Node,
    },


    onLoad() {
        this.playBtn.on("click", this._close, this);
        for (let i = 0; i < this.layoutNode.childrenCount; i++) {
            this.layoutNode.children[i].on("click", this._click, this);
        }

        this.maxMoney = Utils.getMaxCash();

        // TODO 广告 Banner
        // GameApp.adSdk.showBanner();
    },

    showUI() {


        // redActive:[false,false,false,false],// 存红包活动是否已领取

        let hideNums = 0;
        let redActiveData = Utils.getRedActive();
        for (let i = 0; i < redActiveData.length; i++) {
            if(redActiveData[i] == false){
                this.layoutNode.children[i].active = true;

            } else {
                this.layoutNode.children[i].active = false;
            }
        }

        // 改变layout
        if (hideNums >= redActiveData.length - 1) {
            this.layoutNode.width = this.layoutNode.children[0].width;
        }

        // 100, 150, 200, 300

        let money = Utils.getRedPacketNum();
        let num = 0;
        let maxNum = 0;
        if (money < 100) {
            maxNum = 100
        } else if (money >= 100 && money < 150) {
            maxNum = 150;
        } else if (money >= 150 && money < 200) {
            maxNum = 40;
        } else if (money >= 200 && money < 300) {
            maxNum = 300;
        } else {
            ADSdk.showToast("数据异常");
        }
        num = Number(maxNum)  - Number(money) ;

        this.desLabel_1.string = "当前流水" + money.toFixed(2) + "，还差" + num.toFixed(2) + "拆";
        this.desLabel_2.string = maxNum + "元红包";
    },


    updateUI() {

    },

    Show() {
        this.showUI();
        this.node.active = true;
    },

    Close() {
        this.node.active = false;
    },

    _close() {
        // this.node.removeFromParent();
        this.Close();
    },

    _click(event) {

        let arr1 = [10, 20, 40, this.maxMoney];
        // let arr2 = [8, 18, 28, 88];
        let id;
        let name = event.name;
        switch (name) {
            case "red1<Button>":
                id = 0;
                break;
            case "red2<Button>":
                id = 1;
                break;
            case "red3<Button>":
                id = 2;
                break;
            case "red4<Button>":
                id = 3;
                break;
        }

        let money = Utils.getRedPacketNum();
        if (money >= arr1[id]) {
            let randomNum = Utils.getRandomRedNum();
            if (money + randomNum >= this.maxMoney) {
                ADSdk.showToast("数据异常");
            } else {
                Utils.addRedPacketNum(randomNum);
                Utils.setRedActive(id)
                UIManage.Instance.ShowSmallPacket((src)=>{
                    src.init(randomNum)
                });
                this._close();
            }

        }else{
            ADSdk.showToast("数据异常");
        }

    }


    // update (dt) {},
});
